package com.gzz.orchid.common.core.util;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * JDK11
 *
 * @author higmd
 */
public class HttpUtil {
    private final HttpClient INSTANCE = HttpClient.newBuilder()
            // 5秒
            .connectTimeout(Duration.ofMillis(5000))
            // 指定httpClinet使用的http协议版本
            .version(HttpClient.Version.HTTP_2)
            .build();

    /**
     *
     */
    public void sendGet(URI uri) throws ExecutionException, InterruptedException {

        //创建请求。get请求方式
        HttpRequest request = HttpRequest.newBuilder()
                .timeout(Duration.ofMillis(5000))
                .uri(uri)
                // 默认就是get方式，可省略
                // .GET()
                // 配置项
                .header("key1", "value1")
                .header("key2", "value2")
                .build();
        // 同步请求
        // HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        // System.out.println(response.body());


        // 异步请求
        CompletableFuture<String> completableFuture = INSTANCE.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body);


        //使用响应。completableFuture.get()会阻塞当前线程，直到请求完成返回了响应，或者超时
        String resposeBody = completableFuture.get();
        System.out.println(resposeBody);
    }

    public void sendGetAysnc(URI uri) throws ExecutionException, InterruptedException {

        //创建请求。get请求方式
        HttpRequest request = HttpRequest.newBuilder()
                .timeout(Duration.ofMillis(5000))
                .uri(uri)
                // 默认就是get方式，可省略
                // .GET()
                // 配置项
                .header("key1", "value1")
                .header("key2", "value2")
                .build();
        // 同步请求
        // HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        // System.out.println(response.body());


        // 异步请求
        CompletableFuture<String> completableFuture = INSTANCE.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body);


        //使用响应。completableFuture.get()会阻塞当前线程，直到请求完成返回了响应，或者超时
        String resposeBody = completableFuture.get();
        System.out.println(resposeBody);
    }

    /**
     *
     */
    public void sendPost(URI uri, String jsonData) throws ExecutionException, InterruptedException {

        //创建请求。get请求方式
        HttpRequest request = HttpRequest.newBuilder()
                .timeout(Duration.ofMillis(5000))
                .uri(uri)
                // 默认就是get方式，可省略
                // .GET()
                // 配置项
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonData))
                .build();
        // 同步请求
        // HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        // System.out.println(response.body());


        // 异步请求
        CompletableFuture<String> completableFuture = INSTANCE.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body);


        //使用响应。completableFuture.get()会阻塞当前线程，直到请求完成返回了响应，或者超时
        String resposeBody = completableFuture.get();
        System.out.println(resposeBody);
    }

}
